from cat_dog_torch.settings import *
from cat_dog_torch.model import AlexNet
from cat_dog_torch.loaders import val_dataloader, test_dataloader
import logging

logging.basicConfig(level=logging.INFO)

if __name__ == '__main__':

    model = AlexNet().cuda()
    model.eval()
    model.load_state_dict(torch.load(MODEL_NAME))
    logging.info('load cnn model')

    # correct_pred 统计正确预测的样本数，num_examples 统计样本总数
    correct_pred, num_examples = 0, 0
    for i, (features, targets) in enumerate(val_dataloader):
        features = features.to(DEVICES)
        targets = targets.to(DEVICES)

        logits = model(features)
        # tensor转预测值 它的size跟label相同 [32]长度向量
        _, predicted_labels = torch.max(logits, 1)

        font = '狗' if predicted_labels[0] == 1 else '猫'
        logging.info('{} 是: {}'.format(targets[0], font))

        num_examples += targets.size(0)
        assert predicted_labels.size() == targets.size()
        correct_pred += (predicted_labels == targets).sum()

    # tensor数据使用item()进行转换成为一个值
    print((correct_pred.float() / num_examples * 100).item())
